package com.lemon.smartsafetynew.ui.fragment.record;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.record.AmmonVo;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.net.services.RecordServices;
import com.lemon.smartsafetynew.ui.adapter.record.AmmonInfoAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshAddTitleFragment;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by JiangAn on 2018/5/24.
 */

public class RecordAmmonFragment extends BasePullToRefreshAddTitleFragment<AmmonVo, List<AmmonVo>>
        implements RecyclerTClickListener<AmmonVo> {

    private static MeCompanyVo companyVo;
    private Dialog dialog;

    public static RecordAmmonFragment newInstance(MeCompanyVo companyVo) {
        Bundle args = new Bundle();
        RecordAmmonFragment fragment = new RecordAmmonFragment();
        args.putSerializable(KeyConstants.KEY_COMPANY_VO, companyVo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_COMPANY_VO)) {
                companyVo = (MeCompanyVo) bundle.getSerializable(KeyConstants.KEY_COMPANY_VO);
            }
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        llTilte.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.company_space_count);
    }

    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", companyVo.getCompany_id());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        RecordServices.init().getAmmoniaList(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", companyVo.getCompany_id());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);
        RecordServices.init().getAmmoniaList(RefreshType.LOAD_MORE, params);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<AmmonVo> getLemonAdapter() {
        return new AmmonInfoAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_AMMONIA_LIST;
    }

    @Override
    public void onDataList(List<AmmonVo> data) {
        if (data != null && data.size() > 0) {
            tvTitleCount.setText("" + data.get(0).getTotalNumber());
        } else {
            tvTitleCount.setText("0");
        }
    }

    @Override
    public void onItemClick(AmmonVo data, int position) {
        initDialog(data);
    }

    private void initDialog(AmmonVo data) {

        dialog = new Dialog(getmContext(), R.style.dialog_style);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_ammon, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_cancel);
        TextView remarkTv = (TextView) view.findViewById(R.id.tv_content);

        remarkTv.setText(CommonUtil.avoidNullString(data.getRemark()));

        dialog.setContentView(view);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}


